<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Formats an object</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="intldateformatter.format.html">« IntlDateFormatter::format</a></li>
      <li style="float: right;"><a href="intldateformatter.getcalendar.html">IntlDateFormatter::getCalendar »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.intldateformatter.html">IntlDateFormatter</a></li>
    <li>Formats an object</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="intldateformatter.formatobject" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">IntlDateFormatter::formatObject</h1>
  <h1 class="refname">datefmt_format_object</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8, PECL intl &gt;= 3.0.0)</p><p class="refpurpose"><span class="refname">IntlDateFormatter::formatObject</span> -- <span class="refname">datefmt_format_object</span> &mdash; <span class="dc-title">Formats an object</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-intldateformatter.formatobject-description">
  <h3 class="title">说明</h3>
  <p class="para">
   面向对象风格
  </p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><strong>IntlDateFormatter::formatObject</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.intlcalendar.html" class="type IntlCalendar">IntlCalendar</a></span>|<span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span></span> <code class="parameter">$datetime</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">int</span>|<span class="type">string</span>|<span class="type">null</span></span> <code class="parameter">$format</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$locale</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   过程化风格
  </p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>datefmt_format_object</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.intlcalendar.html" class="type IntlCalendar">IntlCalendar</a></span>|<span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span></span> <code class="parameter">$datetime</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">int</span>|<span class="type">string</span>|<span class="type">null</span></span> <code class="parameter">$format</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$locale</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  
  <p class="para rdfs-comment">
   This function allows formatting an <span class="classname"><a href="class.intlcalendar.html" class="classname">IntlCalendar</a></span> or
   <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> object without first explicitly creating a
   <span class="classname"><a href="class.intldateformatter.html" class="classname">IntlDateFormatter</a></span> object.
  </p>
  <p class="para">
   The temporary <span class="classname"><a href="class.intldateformatter.html" class="classname">IntlDateFormatter</a></span> that will be created
   will take the timezone from the passed in object. The timezone database
   bundled with PHP will not be used – ICU&#039;s will be used instead. The timezone
   identifier used in <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> objects must therefore
   also exist in ICU&#039;s database.
  </p>
  
 </div>

 
 <div class="refsect1 parameters" id="refsect1-intldateformatter.formatobject-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">datetime</code></dt>

    <dd>

     <p class="para">
      An object of type <span class="classname"><a href="class.intlcalendar.html" class="classname">IntlCalendar</a></span> or
      <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span>. The timezone information in the object
      will be used.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">format</code></dt>

    <dd>

     <p class="para">
      How to format the date/time. This can either be an <span class="type">array</span> with
      two elements (first the date style, then the time style, these being one
      of the constants <strong><code>IntlDateFormatter::NONE</code></strong>,
      <strong><code>IntlDateFormatter::SHORT</code></strong>,
      <strong><code>IntlDateFormatter::MEDIUM</code></strong>,
      <strong><code>IntlDateFormatter::LONG</code></strong>,
      <strong><code>IntlDateFormatter::FULL</code></strong>), an <span class="type">int</span> with
      the value of one of these constants (in which case it will be used both
      for the time and the date) or a <span class="type">string</span> with the format
      described in <a href="https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax" class="link external">&raquo;&nbsp;the ICU
      documentation</a>. If <strong><code>null</code></strong>, the default style will be used.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">locale</code></dt>

    <dd>

     <p class="para">
      The locale to use, or <strong><code>null</code></strong> to use the <a href="intl.configuration.html#ini.intl.default-locale" class="link">default one</a>.
     </p>
    </dd>

   
  </dl>

 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-intldateformatter.formatobject-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   A string with result 或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>

 
 
 <div class="refsect1 examples" id="refsect1-intldateformatter.formatobject-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2425">
    <p><strong>示例 #1 <span class="function"><strong>IntlDateFormatter::formatObject()</strong></span> examples</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;default&nbsp;timezone&nbsp;is&nbsp;irrelevant;&nbsp;timezone&nbsp;taken&nbsp;from&nbsp;the&nbsp;object&nbsp;*/<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'date.timezone'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'UTC'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">/*&nbsp;default&nbsp;locale&nbsp;is&nbsp;taken&nbsp;from&nbsp;this&nbsp;ini&nbsp;setting&nbsp;*/<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'intl.default_locale'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'fr_FR'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$cal&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">IntlCalendar</span><span style="color: #007700">::</span><span style="color: #0000BB">fromDateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"2013-06-06&nbsp;17:05:06&nbsp;Europe/Dublin"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"default:\n\t"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">formatObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$cal</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"long&nbsp;\$format&nbsp;(full):\n\t"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">formatObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$cal</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">FULL</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"array&nbsp;\$format&nbsp;(none,&nbsp;full):\n\t"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">formatObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$cal</span><span style="color: #007700">,&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">NONE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">FULL</span><span style="color: #007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"string&nbsp;\$format&nbsp;(d&nbsp;'of'&nbsp;MMMM&nbsp;y):\n\t"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">formatObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$cal</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"d&nbsp;'of'&nbsp;MMMM&nbsp;y"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'en_US'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"with&nbsp;DateTime:\n\t"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">formatObject</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"2013-09-09&nbsp;09:09:09&nbsp;Europe/Madrid"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">FULL</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'es_ES'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
default:
    6 juin 2013 17:05:06
long $format (full):
    jeudi 6 juin 2013 17:05:06 heure d’été irlandaise
array $format (none, full):
    17:05:06 heure d’été irlandaise
string $format (d &#039;of&#039; MMMM y):
    6 of June 2013
with DateTime:
    lunes, 9 de septiembre de 2013 09:09:09 Hora de verano de Europa central

</pre></div>
    </div>
   </div>
  </p>
 </div>

 
</div></div></div></body></html>